package com.demo.chapter11future.sync;

import java.util.Random;
import static com.demo.chapter11future.Util.*;

//最佳价格查询器
//会查询多个在线商店，根据给定的产品和服务找出最低价格
public class Shop {
	
	private final String name;
    private final Random random;

    public Shop(String name) {
        this.name = name;
        random = new Random(name.charAt(0) * name.charAt(1) * name.charAt(2));
    }

    //依据指定产品名称返回价格
    public String getPrice(String product) {
        double price = calculatePrice(product);
        Discount.Code code = Discount.Code.values()[random.nextInt(Discount.Code.values().length)];
        return name + ":" + price + ":" + code;
    }
    
    //依据指定产品名称返回价格
    public String getPrice2(String product) {
    	double price = calculatePrice2(product);
        Discount.Code code = Discount.Code.values()[random.nextInt(Discount.Code.values().length)];
        return name + ":" + price + ":" + code;
    }

    //调用delay，并返回一个随机计算的值，模拟从数据库或从其它网站获取产品价格
    public double calculatePrice(String product) {
        delay();
        return format(random.nextDouble() * product.charAt(0) + product.charAt(1));
    }
    
    //调用delay，并返回一个随机计算的值，模拟从数据库或从其它网站获取产品价格
    public double calculatePrice2(String product) {
    	delay2();
    	return format(random.nextDouble() * product.charAt(0) + product.charAt(1));
    }

    public String getName() {
        return name;
    }

}
